import { JxbHttpClient } from '../../data/session/session.js'
const Common = require('../../utils/common.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    product: {
      type: Object,
      value: {}
    },
    quantity:{
      type:Number,
      value: 1
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    total: 0,
    gross: 0,
    cartList: []
  },

  pageLifetimes: {
    show() {
     this.queryCart()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    hideCartpop() {
      this.setData({
        show: true
      })
    },
    updateNum(e) {
      let num = e.detail.value;
      const tnum = parseInt(num);
      if (num.length == 0) {
        this.setData({
          chosenNum: 1
        })
      } else {
        this.setData({
          quantity: tnum
        })
      }
    },
    toZero: function (e) {
      var num = e.detail.value;
      if (num.length == 0) {
        this.setData({
          chosenNum: 1
        })
      }
    },
    //前往购物车
    goCarts: function () {
      wx.switchTab({
        url: '/pages/shoppingcart/index'
      })
    },
    addToCartSingle (){
      this.triggerEvent('addNum')
    },
    reduceSingle (){
      this.triggerEvent('minusNum')
    },
    addCart(){
      this.triggerEvent('addCart')
    },
    queryCart() {
      new JxbHttpClient().get('/AppMaster/PosCart/cartDetail').then(jxbResp => {
        if (typeof jxbResp.data === 'object' && Object.keys(jxbResp.data).length > 0) {
          const { productsList, activity_list } = jxbResp.data
          var total = 0, gross = 0.00;
          var itemids = [];
          var limits = [];
          for (var z = 0; z < activity_list.length; z++) {
            if (activity_list[z].activity_category == 6) {
              itemids = itemids.concat(list[z].itemids);
            }
            if (activity_list[z].activity_category == 4) {
              limits = limits.concat(list[z].itemids);
            }
          }
          for (var i = 0; i < productsList.length; i++) {
            total = total + productsList[i].carNumber;
            gross = gross + productsList[i].carNumber * productsList[i].show_price;
            var z = Common.contains(itemids, productsList[i].itemid),
              w = Common.contains(limits, productsList[i].itemid);
            if (z && w) {
              productsList[i].activity_list = ['秒杀', '限购']
            } else {
              if (z) {
                productsList[i].activity_list = ['秒杀'];
              }
              if (w) {
                productsList[i].activity_list = ['限购'];
              }
            }
          }
          gross = gross.toFixed(2);
          this.setData({
            cartList: productsList,
            total,
            gross
          })
          if (productsList.length > 0) this.triggerEvent('minusHeight')
        } else {
          this.setData({
          })
        }
      }).catch(err=>{
        console.log(err)
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000
        })
      })
    }
  }
})
